package com.broadvision.bvgs.multilang.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionMapping;

import com.broadvision.data.client.Category;
import com.broadvision.data.client.Content;
import com.broadvision.data.client.DataManager;
import com.broadvision.data.common.ContentKey;
import com.broadvision.data.exceptions.CategoryNotFoundException;
import com.broadvision.data.exceptions.DataDeadLockedException;
import com.broadvision.data.exceptions.DataException;
import com.broadvision.portal.services.Channel;
import com.broadvision.portal.services.PortalDataException;
import com.broadvision.portal.services.PortalVisitor;
import com.broadvision.portal.services.util.TransferBeanUtils;
import com.broadvision.portal.tools.action.channel.ChannelBrowseAction;
import com.broadvision.portal.tools.action.channel.ChannelUtils;
import com.broadvision.system.accesscontrol.client.AccessManager;
import com.broadvision.web.util.ViewDynaBean;

// Referenced classes of package com.broadvision.portal.tools.action.channel:
//            ChannelUtils

public class ChannelMultilangViewAction extends IMultilangValueObjectMultilangViewAction
{

    protected void prepareViewBeans(ActionMapping mapping, HttpServletRequest request, int serviceId, PortalVisitor portalVisitor, Content content)
        throws Exception
    {
    	
    	System.out.println("inside class: "+ this.getClass());
    	com.broadvision.portal.tools.action.channel.ChannelBrowseAction c = new ChannelBrowseAction();
        super.prepareViewBeans(mapping, request, serviceId, portalVisitor, content);
        prepareContext(mapping, request);
        Channel channel = new Channel(content.getServiceId(), content.getOid(), portalVisitor);
        ViewDynaBean view = prepareChannelBean(mapping, channel);
        request.setAttribute(VIEW_BEAN_NAME, view);
        AccessManager accessMgr = AccessManager.instance();
        com.broadvision.visitor.client.Visitor visitor = portalVisitor.getVisitor();
        boolean isChannelAdmin = ChannelUtils.isChannelAdmin(serviceId, visitor);
        if(isChannelAdmin || accessMgr.isPermitted(visitor, channel, "Manage"))
            PropertyUtils.setSimpleProperty(view, "editable", Boolean.TRUE);
        Map contextParams = prepareContextParams(request);
        request.setAttribute("pagetabParam", contextParams);
    }

    protected ViewDynaBean prepareChannelBean(ActionMapping mapping, Channel channel)
        throws Exception
    {
        ViewDynaBean view = new ViewDynaBean(VIEW_BEAN_NAME, mapping);
        TransferBeanUtils.copyPropertiesToView(view, channel);
        return view;
    }

    protected Channel getChannelValue(HttpServletRequest request)
        throws Exception
    {

    	System.out.println("inside method getChannelValue() of class: "+this.getClass());
    	System.out.println("9");
        int serviceId = getServiceId(request);
        System.out.println("10");
        PortalVisitor portalVisitor = getPortalVisitor(request);
        System.out.println("11");
        String parameter = request.getParameter("channelId");
        System.out.println("12");
        
        System.out.println("parameter: "+parameter);
        
        long channelId = Long.parseLong(parameter);
        System.out.println("13");
        Channel channel = new Channel(serviceId, channelId, portalVisitor);
        System.out.println("14");
        return channel;
    }


    protected void prepareContext(ActionMapping mapping, HttpServletRequest request)
    {
        String ctxKey = request.getParameter("ctx.f");
        request.setAttribute("ctx.f", ctxKey);
    }

    protected Map prepareContextParams(HttpServletRequest request)
    {
        Map map = new HashMap();
        for(int index = 0; index < PARAM_NAMES.length; index++)
        {
            String name = PARAM_NAMES[index];
            map.put(name, request.getParameter(name));
        }

        return map;
    }
    
    protected Content getContentToTranslate(HttpServletRequest request){
        try {
        	System.out.println("inside method getContentToTranslate() of class: "+this.getClass());
			int serviceId = getServiceId(request);
			
			System.out.println("1");
			Channel channel = getChannelValue(request);
			System.out.println("2");
			String channelPath = channel.getPathName();
			System.out.println("3");
			DataManager dataMgr = new DataManager();
			ContentKey categoryKey = new com.broadvision.data.common.ContentKey(6,serviceId,channelPath,"KM_PROGRAM");
			Category category = dataMgr.getCategory(categoryKey);
			return category;
		} catch (DataDeadLockedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CategoryNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PortalDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }

    
    protected String getTranslationTableName(){
	return TRANSLATION_TABLE_NAME;
    }
    protected String getTranslationLanguageFieldName(){
    	return TRANSLATION_LANGUAGE_FIELD_NAME;
    }
    protected String getTranslationNameFieldName(){
    	return TRANSLATION_NAME_FIELD_NAME;
    }

    public static String VIEW_BEAN_NAME = "epChannelView";
    public static String PARAM_NAMES[] = {
        "channelId", "ctx.f"
    };
    public static String TRANSLATION_TABLE_NAME = "CATEGORY_CONTENT_TRANSLATE";
    public static String TRANSLATION_LANGUAGE_FIELD_NAME = "CATEGORY_CONTENT_LANGUAGE";
    public static String TRANSLATION_NAME_FIELD_NAME = "NAME_TRANSLATE";
}

